package rest

import (
	"strconv"

	"github.com/labstack/echo/v4"

	"bmember/pkg/lueerror"

	pb "bmember/api/pb/server"

	"bmember/internal/service"
)

func (hl *mHandler) adminRegister(r *echo.Group) {
	// 更换会员卡
	r.PUT("/v1/card/update", hl.changeMemberCard)
	// 管理后台客户查询
	r.GET("/v1/consumers/search", hl.searchMembers)
	// 获取用户详情
	r.GET("/v1/consumers/:member_id", hl.memberDetail)
	// 获取用户账号信息
	r.GET("/v1/consumers/:member_id/account", hl.memberAccount)
	// 用户消费信息
	r.GET("/v1/consumers/:member_id/consumption", hl.memberConsumptionData)
	// 用户积分记录
	r.GET("/v1/consumers/:member_id/integrals", hl.memberIntegrals)
	// 用户概况
	r.GET("/v1/statistic/summery", hl.statisticSummery)
	//用户余额记录
	r.GET("/v1/consumers/:member_id/balance-logs", hl.memberBalanceLogs)
}

func (hl *mHandler) changeMemberCard(c echo.Context) error {
	ctx := c.Request().Context()
	token, errL := hl.AdminPermissionCheck(c, "member")
	if errL != nil {
		return errL
	}
	req := &pb.UpdateMemberAccountCardReq{}
	if errL = hl.ParseBody(c, req); errL != nil {
		return errL
	}
	errL = service.GetMemberSrv().MemberCardChanging(ctx, token.BrandId, req.MemberId, req.CardId)
	return hl.SimpleRet(c, nil, errL)
}

func (hl *mHandler) searchMembers(c echo.Context) error {
	ctx := c.Request().Context()
	token, errL := hl.AdminPermissionCheck(c, "member")
	if errL != nil {
		return errL
	}
	cardId, errL := parseInt64Query(c, "card_id")
	if errL != nil {
		return errL
	}
	memberId, errL := parseInt64Query(c, "member_id")
	if errL != nil {
		return errL
	}
	nickname := c.QueryParam("nickname")
	mobile := c.QueryParam("mobile")
	if mobile != "" {
		if errL = hl.MobileCheck(mobile); errL != nil {
			return errL
		}
	}
	pg := paginationParse(c)
	ret, errL := service.GetMemberSrv().AdminSearchMembers(ctx, token.BrandId, cardId, memberId, nickname, mobile, pg)
	return hl.SimpleRet(c, ret, errL)
}

func (hl *mHandler) memberDetail(c echo.Context) error {
	ctx := c.Request().Context()
	token, errL := hl.AdminPermissionCheck(c, "member")
	if errL != nil {
		return errL
	}
	member := c.Param("member_id")
	memberId, err := strconv.ParseInt(member, 10, 64)
	if err != nil {
		return lueerror.InvalidRequest(err)
	}
	ret, errL := service.GetMemberSrv().AdminGetMemberInfo(ctx, token.BrandId, memberId)
	return hl.SimpleRet(c, ret, errL)
}

func (hl *mHandler) memberAccount(c echo.Context) error {
	ctx := c.Request().Context()
	token, errL := hl.AdminPermissionCheck(c, "member")
	if errL != nil {
		return errL
	}
	member := c.Param("member_id")
	memberId, err := strconv.ParseInt(member, 10, 64)
	if err != nil {
		return lueerror.InvalidRequest(err)
	}
	ret, errL := service.GetMemberSrv().GetMemberAccount(ctx, token.BrandId, memberId)
	return hl.SimpleRet(c, ret, errL)
}

func (hl *mHandler) memberConsumptionData(c echo.Context) error {
	ctx := c.Request().Context()
	token, errL := hl.AdminPermissionCheck(c, "member")
	if errL != nil {
		return errL
	}
	member := c.Param("member_id")
	memberId, err := strconv.ParseInt(member, 10, 64)
	if err != nil {
		return lueerror.InvalidRequest(err)
	}
	ret, errL := service.GetMemberSrv().GetMemberConsumptionData(ctx, token.BrandId, memberId)
	return hl.SimpleRet(c, ret, errL)
}

func (hl *mHandler) memberIntegrals(c echo.Context) error {
	ctx := c.Request().Context()
	token, errL := hl.AdminPermissionCheck(c, "member")
	if errL != nil {
		return errL
	}
	member := c.Param("member_id")
	memberId, err := strconv.ParseInt(member, 10, 64)
	if err != nil {
		return lueerror.InvalidRequest(err)
	}
	// 接口支付分页
	pg := paginationParse(c)
	ret, errL := service.GetAssetSrv().GetIntegralRecords(ctx, token.BrandId, memberId, pg)
	return hl.SimpleRet(c, ret, errL)
}

func (hl *mHandler) statisticSummery(c echo.Context) error {
	ctx := c.Request().Context()
	token, errL := hl.AdminPermissionCheck(c, "member")
	if errL != nil {
		return errL
	}
	ret, errL := service.GetMemberSrv().GetAdminMemberSummery(ctx, token.BrandId)
	return hl.SimpleRet(c, ret, errL)
}

func (hl *mHandler) memberBalanceLogs(c echo.Context) error {
	ctx := c.Request().Context()
	token, errL := hl.AdminPermissionCheck(c, "member")
	if errL != nil {
		return errL
	}
	member := c.Param("member_id")
	memberId, err := strconv.ParseInt(member, 10, 64)
	if err != nil {
		return lueerror.InvalidRequest(err)
	}
	// 接口支付分页
	pg := paginationParse(c)
	ret, errL := service.GetAssetSrv().GetBalanceRecords(ctx, token.BrandId, memberId, pg)
	return hl.SimpleRet(c, ret, errL)
}
